home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17289 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.9 KB  |  123 lines

  1. Path: news.gate.net!stpfl2-14
  2. From: rmcinnis@gate.net (Robert McInnis)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Callback Functions In C++
  5. Date: Mon, 15 Apr 96 04:08:56 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4ksinf$ink@news.gate.net>
  8. References: <4kdrfu$kiu@morse.ukonline.co.uk>
  9. NNTP-Posting-Host: stpfl2-14.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4kdrfu$kiu@morse.ukonline.co.uk>, andy.walsh@ukonline.co.uk wrote:
  13. >Can anyone tell me how I could code a callback function that works
  14. >inside a class and that I can pass the address of to the waveInOpen
  15. >function.  It would seem that a member function carries an extra
  16. >'this' pointer and so cannot be addressed the same as an ordinary
  17. >function.  Any ideas?  I also tried to use Borland's response table
  18. >to handle the messages but it will only respond to a 'SendMessage'
  19. >command and not to my low level audio commands.
  20. >
  21.  
  22. What you want to do is code a static in your class that has the following 
  23. signature:
  24.  
  25. static void CALLBACK WaveProc( HWAVE hW, UINT msg, DWORD inst, 
  26.                                DWORD P1, DWORD P2 ) ;
  27.  
  28. According to the Win32 docs, this is what you get:
  29.  
  30.     HWAVE  hW,   // handle of waveform device
  31.     UINT   msg,  // sent message
  32.     DWORD  inst, // instance data
  33.     DWORD  P1,   // application-defined parameter
  34.     DWORD  P2    // application-defined parameter
  35.  
  36. I would probably forget about P1 && P2, I haven't a clue, off the top of my 
  37. head, how to set them.  The things you can do in this function, or any 
  38. functions it calls, is restricted to certain Windows API calls.  The only API 
  39. functions you can call are:
  40.  
  41.   EnterCriticalSection  PostThreadMessage
  42.   LeaveCriticalSection  SetEvent
  43.   midiOutLongMsg        timeGetSystemTime
  44.   midiOutShortMsg       timeGetTime
  45.   OutputDebugString     timeKillEvent
  46.   PostMessage           timeSetEvent
  47.  
  48.  
  49. Next, register the function.  This is the key:
  50.  
  51. waveInOpen( lphWaveIn, IDDevice, lpwf, dwCallback, dwCallbackInst, fdwOpen );
  52.  
  53. When using this with the following class, 
  54.  
  55.   class MCIResponse
  56.   {
  57.     private   :
  58.       LPHWAVEIN    _waveIn ; // handle to the input device
  59.       UINT         _devID ;  // identifier of the device
  60.       WAVEFORMAT   _wf ;     // address of structure with device format
  61.  
  62.     protected :
  63.       static void CALLBACK WaveProc( ... ) ;
  64.  
  65.     public    :
  66.  
  67.       .. (other methods) ..
  68.  
  69.       void                 hookUp() ;
  70.       virtual void         process( HWAVE hW, UINT msg ) ;
  71.   } ;
  72.  
  73. Hook up the function as such:
  74.  
  75. void MCIResponse :: hookUp() 
  76. {
  77.   DWORD   cbFunc, flag, instData ;
  78.  
  79.   cbFunc   = (DWORD) MCIResponse :: WaveProc ;
  80.   instData = (DWORD) this ; // important part!!!
  81.   flag     = CALLBACK_FUNCTION ;
  82.  
  83.   waveInOpen( _waveIn, _devID, &_wf, cbFunc, instData, flag ) ;
  84. } // MCIResponse :: hookUp
  85.  
  86. And when you process the message in WaveProc, do this:
  87.  
  88. void CALLBACK MCIResponse :: WaveProc( HWAVE hW, UINT msg, DWORD inst, 
  89.                                        DWORD P1, DWORD P2 ) ;
  90. {
  91.   MCIResponse *mci = (MCIResponse*)inst ;
  92.   if (mci == NULL)
  93.     return ;  
  94.  
  95.   mci->process( hW, msg ) ;
  96. } // MCIResponse :: WaveProc
  97.  
  98. And that's it.  The "this" pointer came through as data, and then you 
  99. casted it to your class, then used the public methods to do the rest.  Again, 
  100. I haven't a clue about P1 && P2, sorry.
  101.  
  102. Hope this helps,
  103.  
  104. Rob
  105.  
  106.  
  107.  
  108. _________________________________________________________________________
  109. Robert McInnis
  110. (e) rmcinnis@gate.net
  111. http://www.gate.net/~rmcinnis
  112.  
  113. Legal stuff:
  114.  
  115. All opinions are my own, unless incorrect in which case it was the work of
  116. my moronic alter-ego, that does get out once in a while :)
  117.  
  118. Microsoft Network is prohibited from redistibuting this work in any form,
  119. in whole or in part. Copyright, Robert McInnis, 9th Bit Software, 1995
  120. License to distribute this post is available for $1,000. Posting without
  121. permission constitutes an agreement to these terms.  Please send notices
  122. of violation to postmaster@microsoft.com and rmcinnis@gate.net.
  123.